package edu.task.snake.view;

import edu.task.snake.controller.GameEngine;
import edu.task.snake.model.Arena;
import edu.task.snake.model.Frog;
import edu.task.snake.model.Position;

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.util.List;


public class ViewArena extends JPanel {

    private SnakeRender snakeRender;
    private FrogRender frogRender;
    private List<Frog> frogList;
    Arena arena;


    public ViewArena(int b_width,int b_height,SnakeRender snakeRender,FrogRender frogRender,List<Frog> frogList, Arena arena) {
        setBackground(Color.black);
        setFocusable(true);
        setPreferredSize(new Dimension(b_width, b_height));
        this.frogList = frogList;
        this.snakeRender=snakeRender;
        this.frogRender = frogRender;
        this.arena = arena;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        for (Position frog : frogList) {
            frogRender.render(g,frog);
        }
        snakeRender.render(g);

    }


    public void render() {

        this.paintComponent(this.getGraphics());
    }

    public void gameOver() {
        String msg = "Game Over";
        Font small = new Font("Helvetica", Font.BOLD, 14);
        FontMetrics metr = getFontMetrics(small);
        Graphics g = getGraphics();
        g.setColor(Color.white);
        g.setFont(small);
        g.drawString(msg, (arena.getWidth() * GameEngine.DOT_SIZE - metr.stringWidth(msg)) / 2, arena.getHeight() * GameEngine.DOT_SIZE / 2);
    }
}
